<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright   Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php $_items = $this->getCreditmemo()->getAllItems() ?>

<div class="fieldset-wrapper">
    <div class="fieldset-wrapper-title">
        <span class="title"><?php echo __('Items to Refund') ?></span>
    </div>

    <?php if (count($_items)) : ?>
    <div class="grid">
        <table cellspacing="0" class="data order-tables">
            <thead>
                <tr class="headings">
                    <th class="col-product"><span><?php echo __('Product') ?></span></th>
                    <th class="col-price"><span><?php echo __('Price') ?></span></th>
                    <th class="col-ordered-qty"><span><?php echo __('Qty') ?></span></th>
                    <?php if ($this->canReturnToStock()) : ?>
                    <th class="col-return-to-stock"><span><?php echo __('Return to Stock') ?></span></th>
                    <?php endif; ?>
                    <th class="col-refund"><span><?php echo __('Qty to Refund') ?></span></th>
                    <th class="col-subtotal"><span><?php echo __('Subtotal') ?></span></th>
                    <th class="col-tax-amount"><span><?php echo __('Tax Amount') ?></span></th>
                    <th class="col-discont"><span><?php echo __('Discount Amount') ?></span></th>
                    <th class="col-total last"><span><?php echo __('Row Total') ?></span></th>
                </tr>
            </thead>
            <?php if ($this->canEditQty()): ?>
            <tfoot>
                <tr>
                    <td colspan="3">&nbsp;</td>
                    <td colspan="3">
                        <?php echo $this->getUpdateButtonHtml() ?>
                    </td>
                    <td colspan="3" class="last">&nbsp;</td>
                </tr>
            </tfoot>
            <?php endif; ?>
            <?php $i=0;foreach ($_items as $_item): ?>
                <?php if ($_item->getOrderItem()->getParentItem()) continue; else $i++; ?>
                <tbody class="<?php echo $i%2?'even':'odd' ?>">
                    <?php echo $this->getItemHtml($_item) ?>
                    <?php echo $this->getItemExtraInfoHtml($_item->getOrderItem()) ?>
                </tbody>
            <?php endforeach; ?>
        </table>
    </div>
    <?php else : ?>
    <div class="no-items">
        <?php echo __('No Items To Refund') ?>
    </div>
    <?php endif; ?>
</div>

<?php $orderTotalBar = $this->getChildHtml('order_totalbar'); ?>

<?php if (!empty($orderTotalBar)): ?>
<div class="fieldset-wrapper">
    <?php echo $orderTotalBar; ?>
</div>
<?php endif; ?>

<div class="clearfix">
    <input type="hidden" name="creditmemo[do_offline]" id="creditmemo_do_offline" value="0" />
    <div class="order-comments-history">
        <div class="fieldset-wrapper">
            <div class="fieldset-wrapper-title"><span class="title"><?php echo __('Credit Memo Comments') ?></span></div>
            <fieldset id="history_form">
                <label class="normal" for="creditmemo_comment_text"><?php echo __('Credit Memo Comments') ?></label><br/>
                <textarea id="creditmemo_comment_text" name="creditmemo[comment_text]" rows="3" cols="5"><?php echo $this->getCreditmemo()->getCommentText(); ?></textarea>
            </fieldset>
        </div>
    </div>
    <div class="order-totals creditmemo-totals">
        <div class="fieldset-wrapper">
            <div class="fieldset-wrapper-title"><span class="title"><?php echo __('Refund Totals') ?></span></div>
            <?php echo $this->getChildHtml('creditmemo_totals') ?>
            <div class="order-totals-bottom">
                <div  class="field choice field-append-comments">
                    <input id="notify_customer" name="creditmemo[comment_customer_notify]" value="1" type="checkbox" />
                    <label for="notify_customer"><span><?php echo __('Append Comments') ?></span></label>
                </div>
                <?php if ($this->canSendCreditmemoEmail()):?>
                <div class="field choice field-email-copy">
                    <input id="send_email" name="creditmemo[send_email]" value="1" type="checkbox" />
                    <label for="send_email"><span><?php echo __('Email Copy of Credit Memo') ?></span></label>
                </div>
                <?php endif; ?>
                <?php echo $this->getChildHtml('submit_before') ?>
                <div class="actions">
                    <?php echo $this->getChildHtml('submit_offline') ?>
                    <?php echo $this->getChildHtml('submit_button') ?>
                    <?php echo $this->getChildHtml('submit_after') ?>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
require(['jquery', 'prototype'], function(jQuery){

//<![CDATA[
var submitButtons = $$('.submit-button');
var updateButtons = $$('.update-button');
var fields = $$('.qty-input');

updateButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});

for(var i=0;i<fields.length;i++){
    fields[i].observe('change', checkButtonsRelation)
    fields[i].baseValue = fields[i].value;
}

function checkButtonsRelation() {
    var hasChanges = false;
    fields.each(function (elem) {
        if (elem.baseValue != elem.value) {
            hasChanges = true;
        }
    }.bind(this));
    if (hasChanges) {
        submitButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});
        updateButtons.each(function (elem) {elem.disabled=false;elem.removeClassName('disabled');});
    }
    else {
        submitButtons.each(function (elem) {elem.disabled=false;elem.removeClassName('disabled');});
        updateButtons.each(function (elem) {elem.disabled=true;elem.addClassName('disabled');});
    }
}

submitCreditMemo = function() {
    if ($('creditmemo_do_offline')) $('creditmemo_do_offline').value=0;
    // Temporary solution will be replaced after refactoring order functionality
    jQuery('#edit_form').triggerHandler('save');
}

submitCreditMemoOffline = function() {
    if ($('creditmemo_do_offline')) $('creditmemo_do_offline').value=1;
    // Temporary solution will be replaced after refactoring order functionality
    jQuery('#edit_form').triggerHandler('save');
}

var sendEmailCheckbox = $('send_email');

if (sendEmailCheckbox) {
    var notifyCustomerCheckbox = $('notify_customer');
    var creditmemoCommentText = $('creditmemo_comment_text');
    Event.observe(sendEmailCheckbox, 'change', bindSendEmail);
    bindSendEmail();
}

function bindSendEmail()
{
    if (sendEmailCheckbox.checked == true) {
        notifyCustomerCheckbox.disabled = false;
        //creditmemoCommentText.disabled = false;
    }
    else {
        notifyCustomerCheckbox.disabled = true;
        //creditmemoCommentText.disabled = true;
    }
}

window.bindSendEmail = bindSendEmail;
window.checkButtonsRelation = checkButtonsRelation;
//]]>

});
</script>
